package com.kt.uokos.yimo.controller;

import com.kt.uokos.yimo.dao.Region;
import com.kt.uokos.yimo.service.RegionService;
import com.kt.uokos.yimo.util.ResponseUtil;
import com.kt.uokos.yimo.vo.RegionVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/region")
@Validated
public class RegionController {
    @Resource
    private RegionService regionService;


    @GetMapping("/list")
    public Object list() {
        ArrayList<RegionVo> regionVOList = new ArrayList<>();
        //获取所有数据
        List<Region> allRegion = regionService.getAll();
        //将所有地区与其相对应的地区类型关联起来
        Map<Byte, List<Region>> collect = allRegion.stream().collect(Collectors.groupingBy(Region::getType));

        byte provinceType = 1;
        //拿到所有的省份
        List<Region> provinceList = collect.get(provinceType);
        byte cityType = 2;
        //拿到所有城市
        List<Region> cityList = collect.get(cityType);
        //将城市与其行政区域父id绑定起来
        Map<Integer, List<Region>> cityListMap = cityList.stream().collect(Collectors.groupingBy(Region::getPid));

        byte areaType = 3;
        //拿到所有区县
        List<Region> areaList = collect.get(areaType);
        //将城市与其行政区域父id绑定起来
        Map<Integer, List<Region>> areaListMap = areaList.stream().collect(Collectors.groupingBy(Region::getPid));
        for (Region province : provinceList) {
            RegionVo provinceVO = new RegionVo();
            //将省份相关信息放入VO
            provinceVO.setId(province.getId());
            provinceVO.setName(province.getName());
            provinceVO.setCode(province.getCode());
            provinceVO.setTybe(province.getType());
            //根据此省份pid获取所有相关城市
            List<Region> citylist = cityListMap.get(province.getId());
            List<RegionVo> cityVOList = new ArrayList<>();
            for (Region city : citylist) {
                RegionVo cityVO = new RegionVo();
                //将城市相关信息放入VO
                cityVO.setId(city.getId());
                cityVO.setName(city.getName());
                cityVO.setCode(city.getCode());
                cityVO.setTybe(city.getType());
                //根据此城市pid获取所有相关区域
                List<Region> arealist = areaListMap.get(city.getId());
                List<RegionVo> areaVOList = new ArrayList<>();
                for (Region area : arealist) {
                    RegionVo areaVO = new RegionVo();
                    //将地区相关信息放入VO
                    areaVO.setId(area.getId());
                    areaVO.setName(area.getName());
                    areaVO.setCode(area.getCode());
                    areaVO.setTybe(area.getType());
                    areaVOList.add(areaVO);
                }
                cityVO.setChildren(areaVOList);
                cityVOList.add(cityVO);
            }
            provinceVO.setChildren(cityVOList);
            regionVOList.add(provinceVO);
        }
        return ResponseUtil.okList(regionVOList);
    }
}